7
/**
 * Created by gengrong on 16/5/9.
 */
import { getFetchStatus } from 'actions/util';
import * as ReactRedux from 'react-redux';
import * as React from 'react';
import PureRunnableGroupConfig from '../components/runnable-group-config/runnable-group-config';
import * as RunnableGroupAction from 'actions/runnable-group';
import * as DevTeamAction from 'actions/dev-team';

const mapStateToProps = (state) => {
  return {
    isFetching: getFetchStatus(state, RunnableGroupAction.get),
    detail: state.runnableGroup,
    devTeamDetail: state.devTeamDetail
  };
};

const mapDispatchToProps = (dispatch, ownProps) => {
  const groupId = ownProps.groupId;

  const loadData = () => {
    return dispatch(RunnableGroupAction.get(groupId))
      .then(action => dispatch(DevTeamAction.getDetail(action.payload.runnableGroup.id)));
  }

  return {
    initFetch: () => {
      loadData();
    },
    onRunnableGroupConfigUpdate: (runnableGroup) => {
      return dispatch(RunnableGroupAction.update(groupId, runnableGroup))
        .then(() => dispatch(RunnableGroupAction.get(groupId)));
    },
    onRunnableGroupAssign: (teamId) => {
      return dispatch(RunnableGroupAction.assign(groupId, teamId))
        .then(() => loadData());
    }
  };
};

class RunnableGroupConfig extends React.Component {
  componentDidMount() {
    this.props.initFetch();
  }

  render() {
    return <PureRunnableGroupConfig {...this.props}/>;
  }
}

export default ReactRedux.connect(mapStateToProps, mapDispatchToProps)(RunnableGroupConfig);
